##############run robustness checks with ANES post-stratification weights#######
################################################################################

#### ---------------2020-WEIGHTED ANALYSIS
######################################
### H1: Immigration as Positional Cue
######################################
model_h1_2020_wt <- glm(vote_choice_binary ~ immigration_ideology,
                        data = anes_2020,
                        family = binomial(link = "logit"),
                        weights = V200010a)

summary(model_h1_2020_wt)

######################################
### H2: Immigration as Hybrid Valence
######################################
model_h1_2024_wt <- glm(vote_choice_binary_post ~ immigration_ideology,
                        data = anes_2024,
                        family = binomial(link = "logit"),
                        weights = V240107a)

summary(model_h1_2024_wt)

#H2:
model_h2_2020_wt <- glm(vote_choice_binary ~ immigration_ideology + rel_comp + rel_honest,
                        data = anes_2020,
                        family = binomial(link = "logit"),
                        weights = V200010a)

summary(model_h2_2020_wt)

######################################
### H3: Issue Absorption
######################################
model_h3_2020_wt <- glm(vote_choice_binary_post ~ immigration_ideology + rel_comp + rel_honest, + pid + immigration_ideology*pid,
                        data = anes_2020,
                        family = binomial(link = "logit"),
                        weights = V200010a)

summary(model_h3_2020_wt)

#### ---------------2024-WEIGHTED ANALYSIS

#H3: (USE THIS FORMAT FOR H1 & H2)
######################################
### H1: Immigration as Positional Cue
######################################
model_h1_2024_wt <- glm(vote_choice_binary_post ~ immigration_ideology,
                        data = anes_2024,
                        family = binomial(link = "logit"),
                        weights = V240107a)

summary(model_h1_2024_wt)

######################################
### H2: Immigration as Hybrid Valence
######################################
model_h2_2024_wt <- glm(vote_choice_binary_post ~ immigration_ideology + rel_comp + rel_honest, + pid,
                        data = anes_2024,
                        family = binomial(link = "logit"),
                        weights = V240107a)

summary(model_h2_2024_wt)

######################################
### H3: Issue Absorption
######################################
model_h3_2024_wt <- glm(vote_choice_binary_post ~ immigration_ideology + rel_comp + rel_honest, + pid + immigration_ideology*pid,
                        data = anes_2024,
                        family = binomial(link = "logit"),
                        weights = V240107a)

summary(model_h3_2024_wt)

sum(anes_2024$V240107a, na.rm=TRUE)
sum(anes_2024, na.rm=TRUE)

################################################################
### FIND the N size FOR 2020 AND 2024 WEIGHTED AND UNWEIGHTED
################################################################
#---2020
# Drop missing weights just to be safe
w0 <- na.omit(anes_2020$V200010a)

# Raw n: number of non-missing observations used
raw_n0 <- length(w0)

# Weighted n: sum of weights
weighted_n0 <- sum(w0)

# Effective n: accounts for unequal weights
eff_n0 <- (sum(w0)^2) / sum(w0^2)

cat("Raw n:", raw_n0, "\n")
cat("Weighted n:", weighted_n0, "\n")
cat("Effective n:", eff_n0, "\n")


#----2024
# Drop missing weights just to be safe
w <- na.omit(anes_2024$V240107a)

# Raw n: number of non-missing observations used
raw_n <- length(w)

# Weighted n: sum of weights
weighted_n <- sum(w)

# Effective n: accounts for unequal weights
eff_n <- (sum(w)^2) / sum(w^2)

cat("Raw n:", raw_n, "\n")
cat("Weighted n:", weighted_n, "\n")
cat("Effective n:", eff_n, "\n")